package com.ershui.blog.system.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

@Service
public class MailUti {
    @Autowired
    private  JavaMailSender mailSender;

    public  void sendMail(String subject,String text,String to,String from){
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件设置
        message.setSubject(subject);
        message.setText(text);
        message.setTo(to);
        message.setFrom(from);
        try {
            mailSender.send(message);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    /**
     * HTML 文本邮件
     * @param to 接收者邮件
     * @param subject 邮件主题
     * @param text HTML内容
     * @throws MessagingException
     */
    public void sendHtmlMail(String subject,String text,String to,String from) {
        MimeMessage message = mailSender.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(text, true);
            helper.setFrom(from);
            mailSender.send(message);
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}
